package com.geen.tuoke.utils.ext

import android.app.Activity
import android.content.Intent
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.geen.tuoke.R
import com.geen.tuoke.base.BaseActivity
import com.qmuiteam.qmui.skin.QMUISkinManager
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet
import com.seeway.wms.utils.putExtras

/**
 * Created by TanJiaJun on 2019-08-07.
 */
inline fun <reified T : BaseActivity<*, *>> Fragment.startActivity() =
    startActivity(android.content.Intent(context, T::class.java))

inline fun <reified TARGET : Activity> Fragment.startActivity(
    vararg params: Pair<String, Any>
) = activity?.run {
    startActivity(Intent(this, TARGET::class.java).putExtras(*params))
}

fun Fragment.showBottomSheetList(
    gravityCenter: Boolean = true,
    addCancelBtn: Boolean = false,
    withIcon: Boolean = false,
    title: CharSequence = "",
    itemList: List<String>,
    allowDragDismiss: Boolean = true,
    withMark: Boolean = false,
    callBack:(index:Int)->Unit
) {
    val builder = QMUIBottomSheet.BottomListSheetBuilder(activity)
    builder.setGravityCenter(gravityCenter)
        .setSkinManager(QMUISkinManager.defaultInstance(activity))
        .setTitle(title)
        .setAddCancelBtn(addCancelBtn)
        .setAllowDrag(allowDragDismiss)
        .setNeedRightMark(withMark)
        .setOnSheetItemClickListener { dialog, itemView, position, tag ->
            dialog.dismiss()
            callBack(position)
        }
    if (withMark) {
        builder.setCheckedIndex(40)
    }
    for (i in itemList.indices) {
        if (withIcon) {
            builder.addItem(
                ContextCompat.getDrawable(context!!.applicationContext, R.mipmap.icon_arrow_right),
                itemList[i]
            )
        } else {
            builder.addItem(itemList[i])
        }
    }
    builder.build().show()
}

